package com.urban.pooling.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.urban.pooling.controller.api.RouteApiController;
import com.urban.pooling.domain.User;
import com.urban.pooling.dto.UserDto;
import com.urban.pooling.repository.UserRepository;

@Service
public class UserService {
	private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);
	@Autowired
	private UserRepository userRepo;

	public User find(int id) {
		return userRepo.findOne(id);
	}

	public User findByUsername(String username) {
		return userRepo.findByUsername(username);
	}
	
	public String save(UserDto newUser){
		try {
			User user= newUser.toUserAndValidate();
			userRepo.save(user);
		} catch (Exception e) {
			LOGGER.debug("User validation failed: " + e.getMessage());
			return  e.getMessage();
		}
		return "";
	}
	
	public User isEmailUnique(String email){
		return userRepo.findByEmail(email);
	}
}
